from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.contrib import admin
from worldcup.apps.userModel.models import Advice
from worldcup.paginator import get_page
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
def view(request):
    return render(request, 'addadvice.html')

def list(request):
    ls = Advice.objects.all().order_by('-id')
    advices = get_page(ls,request)
    dicts={'advices':advices}

    return render(request,'advicelist.html',dicts)


@csrf_exempt
def add(request):

    adviceStr = request.POST.get("advice")

    try:
        openId = request.session["openid"]
    except KeyError:
        return redirect("/")
    else:
        if openId is None:
            return redirect("/")

    advice = Advice()
    advice.openid = openId
    advice.advice = adviceStr
    advice.save()

    return HttpResponseRedirect(reverse('advice_list'))

class AdviceAdmin(admin.ModelAdmin):
    list_display = ('advice',)
